package com.jhws.business.repair.bo;

import com.jhws.core.dataUpdate.bo.DataUpdateObject;
import com.jhws.core.dataUpdate.bo.IDataUpdateModule;
import com.jhws.core.dataUpdate.service.DataUpdateService;
import com.jhws.util.DateUtil;
import com.jhws.util.StringUtils;
import com.jhws.business.repair.service.RepairService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class RepairUpdateData implements IDataUpdateModule {

    static public final String REPAIR = "repair";
    static public final String REPAIR_PROPERTY = "repair_property";
    private String [] name = {REPAIR,REPAIR_PROPERTY};

    @Resource
    RepairService repairService;
    @Resource
    DataUpdateService dataUpdateService;
    @Override
    public String [] getModuleName() {
        return name;
    }

    @Override
    public DataUpdateObject getUserModuleData(String username,String ownerCode, Long timeStamp, String module, Map<String, String> moduleParams) throws Exception {
        if(module.equals(REPAIR)) {
            ownerCode = StringUtils.isEmpty(ownerCode)?moduleParams.get("ownerCode"):ownerCode;
            if (StringUtils.isEmpty(ownerCode))
                return null;
            return new DataUpdateObject(repairService.queryRepairListUpdated(RepairConstant.REPAIR_STATE_ALL, ownerCode, timeStamp));
        }else if(module.equals(REPAIR_PROPERTY)){
            String commCode = moduleParams.get("commCode");
            if (StringUtils.isEmpty(commCode))
                return null;
            int size = repairService.findRepairCount("where comm_code = '"+commCode+"' and timestamp > '"+timeStamp+"'");
            return new DataUpdateObject(null,size);
        }
        return null;
    }

    @Override
    public DataUpdateObject getLabelModuleData(String label, Long timeStamp, String module, Map<String, String> moduleParams) throws Exception {
        return null;
    }

    @Override
    @PostConstruct
    public void registerModule() {
        dataUpdateService.registerModule(this);
    }
}
